package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import connection.ServiceProxy;

/**
 * Servlet implementation class UpdateCart
 */
public class UpdateCart extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateCart() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/home");
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		
		String message = null, result = null, destination = "/ViewCart";
		String itemId =  request.getParameter("itemId");
		String cartItemId = request.getParameter("cartItemId");
		String userId = (String)session.getAttribute("userId");
		
		boolean error = false;
		int orderedQuantity =0, itemQuantity, quantityInCart = 0;
		
		ServiceProxy oProxy = new ServiceProxy();
		
		if(null != cartItemId) {
			
			if(request.getParameter("submit").equals("Delete") || request.getParameter("orderedQuantity").equals("0")){
				result = oProxy.deleteItemInCart(cartItemId);
				if(result.equals("true"))
					message = "Item Deleted Successfully";
				else
					message="Please try again";
			}
			else {
			//int ite
				System.out.println("harr " + request.getParameter("orderedQuantity"));
				if(request.getParameter("orderedQuantity").equals("")) {
				error = true;
				message = "Invalid Quantity";
				}				
				
				if(error == false) {
					try {
						orderedQuantity = Integer.parseInt(request.getParameter("orderedQuantity"));
					}
					catch(NumberFormatException e) {
						error = true;
						message = "Please enter correct value of quantity";
					}
				}
				
				if(error == false) {
					itemQuantity = Integer.parseInt(request.getParameter("itemQuantity"));
				
					if(orderedQuantity > itemQuantity) {
						error = true;
						message = "Order Quantity cannot be greater than Available Quantity";
					}
				}
				
				if(error == false) {
					
					result = oProxy.updateItemInCart(cartItemId,"" + orderedQuantity);
					System.out.println("Result" + result);
					if(result.equals("true")) {
						message = "Item Updated Successfully";					
					}
					else {
						error = true;
						message = "Please try again";
					}
						
				}
			}	
		}
		
		request.setAttribute("message", message);
		RequestDispatcher rd = this.getServletContext().getRequestDispatcher(destination);
		rd.forward(request, response);
		
	}

}
